package com.zry.fertilizer.adapter

import android.content.Context
import android.databinding.adapters.TextViewBindingAdapter.setText
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.zry.fertilizer.R
import com.zry.fertilizer.animation.MyBaseViewHolder
import com.zry.fertilizer.been.*


class HomePageAdapter(layoutResId: Int, data: MutableList<BasicBean>?) :
    BaseQuickAdapter<BasicBean, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: BasicBean?) {
        helper?.setText(R.id.itemHomePageTV, item?.BasicName)
            ?.setBackgroundRes(R.id.itemHomePageImg, item!!.BasicImg)
    }
}

class ProductListAdapter(private val context: Context, layoutResId: Int, data: MutableList<ProductNameBean>?) :
    BaseQuickAdapter<ProductNameBean, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: ProductNameBean?) {
        helper?.setText(R.id.itemProductName, item?.ProductName)
            ?.setText(R.id.itemProductBrand, "产品品牌：" + item?.BrandName)
            ?.setText(R.id.itemProductSpec, "产品规格：" + item?.NetUnitPack)
        helper?.setImgLoad(
            context,
            R.id.itemProductImg,
            item!!.ProductImage,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher
        )
    }
}


class BatchListAdapter(layoutResId: Int, data: MutableList<BatchInfoBean>?, private var title: String) :
    BaseQuickAdapter<BatchInfoBean, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: BatchInfoBean?) {
        when (title) {
            "批次创建" -> {
                helper?.setText(R.id.itemBatchNumber, "批次编码：" + item?.BatchCode)
                    ?.setText(R.id.itemBatchName, item?.BatchName)
                    ?.setText(R.id.itemCreateData, item?.CreateDate)
                    ?.setText(R.id.itemAssociationTemplate, item?.ModelName)
                    ?.setText(R.id.itemAssociatedProducts, item?.ProductName)
            }
            "溯源录入" -> {
                helper?.setText(R.id.itemBatchNumber, "批次编码：" + item?.BatchCode)
                    ?.setText(R.id.itemBatchName, item?.BatchName)
                    ?.setText(R.id.itemCreateDataTV, "产品品牌：")
                    ?.setText(R.id.itemCreateData, item?.BrandName)
                    ?.setText(R.id.itemAssociationTemplateTV, "关联产品：")
                    ?.setText(R.id.itemAssociationTemplate, item?.ProductName)
                    ?.setText(R.id.itemAssociatedProductsTV, "产品规格：")
                    ?.setText(R.id.itemAssociatedProducts, item?.NetUnitPack)
            }
        }
    }
}

class BatchInfoAdapter(layoutResId: Int, data: MutableList<BatchDetail>?) :
    BaseQuickAdapter<BatchDetail, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: BatchDetail?) {
        helper?.setText(R.id.itemBatchPosition, (data.indexOf(item) + 1).toString())
            ?.setText(R.id.itemBatchName, item?.DetailName)
        if (data.indexOf(item) == data.size - 1) {
            helper?.setGone(R.id.itemBatchLine, false)
        }
    }
}

class BatchOPeraAdapter(layoutResId: Int, data: MutableList<BatchOPera>?) :
    BaseQuickAdapter<BatchOPera, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: BatchOPera?) {
        helper?.setText(R.id.itemBatchPosition, (data.indexOf(item) + 1).toString())
            ?.setText(R.id.itemBatchName, item?.DetailName)
            ?.setText(R.id.itemCreateBatchAddress, item?.Address)
            ?.setText(R.id.itemCreateBatchTime, item?.OperaDate)
            ?.setText(R.id.itemCreateBatchContent, item?.Remark)
        if (data.indexOf(item) == data.size - 1) {
            helper?.setGone(R.id.itemBatchLine, false)
        }
    }
}


class PutStorageRecordAdapter(private val context: Context, layoutResId: Int, data: MutableList<StoreOrderBean>?) :
    BaseQuickAdapter<StoreOrderBean, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: StoreOrderBean?) {
        helper?.setTextColorContext(context, R.id.itemPutStorageNumber, R.color.orange)
        helper?.setText(R.id.itemPutStorageNumber, "入库单号：" + item?.StoreOrderCode)
            ?.setText(R.id.itemPutStorageNameTV, "产品名称")
            ?.setText(R.id.itemPutStorageName, item?.ProductName)
            ?.setText(R.id.itemTraceProductBrandTV, "产品规格")
            ?.setText(R.id.itemTraceProductBrand, item?.NetUnitPackName)
            ?.setText(R.id.itemTraceAssociatedProductsTV, "入库数量")
            ?.setText(R.id.itemTraceAssociatedProducts, item?.ProductCount)
            ?.setText(R.id.itemTraceProductPackTV, "入库状态")

        when (item?.OrderState) {
            "0" -> {
                helper?.setText(R.id.itemTraceProductPack, "未结单")
                helper?.setTextColorContext(context, R.id.itemTraceProductPack, R.color.red)
            }
            "1" -> {
                helper?.setText(R.id.itemTraceProductPack, "已结单")
                helper?.setTextColorContext(context, R.id.itemTraceProductPack, R.color.mainBgColor)
            }
        }
    }
}


class OutTreasuryRecordAdapter(private val context: Context, layoutResId: Int, data: MutableList<OutStock>?) :
    BaseQuickAdapter<OutStock, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: OutStock?) {
        helper?.setTextColorContext(context, R.id.itemOutTreasuryNumber, R.color.orange)
        helper?.setText(R.id.itemOutTreasuryNumber, "出库单号：" + item?.OutStockCode)
            ?.setText(R.id.itemDistributorTV, "接收经销商")
            ?.setText(R.id.itemDistributor, item?.CustomerName)
            ?.setText(R.id.itemOutTreasuryStateTV, "出库状态")
            ?.setText(R.id.itemOutTreasuryDataTV, "出库时间")
            ?.setText(R.id.itemOutTreasuryData, item?.CreateDate)
        when (item?.Status) {
            "1", "2" -> {
                helper?.setTextColorContext(context, R.id.itemOutTreasuryState, R.color.red)
                helper?.setText(R.id.itemOutTreasuryState, "未结单")
            }
            "3" -> {
                helper?.setTextColorContext(context, R.id.itemOutTreasuryState, R.color.mainBgColor)
                helper?.setText(R.id.itemOutTreasuryState, "已结单")
            }
            "4" -> {
                helper?.setTextColorContext(context, R.id.itemOutTreasuryState, R.color.mainBgColor)
                helper?.setText(R.id.itemOutTreasuryState, "已收货")
            }
//            "5" -> {
//                helper?.setTextColorContext(context, R.id.itemOutTreasuryState, R.color.red)
//                helper?.setText(R.id.itemOutTreasuryState, "已作废")
//            }
        }
    }
}


class OutgoingListAdapter(layoutResId: Int, data: MutableList<OutgoingBean>?) :
    BaseQuickAdapter<OutgoingBean, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: OutgoingBean?) {
        helper?.setText(R.id.itemOutgoingProductName, item?.ProductName)
            ?.setText(R.id.itemOutgoingSingleProductSpec, item?.NetUnitPack)
            ?.setText(R.id.itemOutgoingOutTreasuryNumber, item?.Quality)
    }
}

class DistributorListAdapter(layoutResId: Int, data: MutableList<CustomerInfo>?) :
    BaseQuickAdapter<CustomerInfo, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: CustomerInfo?) {
        helper?.setText(R.id.itemDistributorNumber, "经销商编号：" + item?.CustomerCode)
            ?.setText(R.id.itemDistributorNameTV, "经销商名称")
            ?.setText(R.id.itemDistributorName, item?.CustomerName)
            ?.setText(R.id.itemDistributorAddressTV, "经销商地址")
            ?.setText(R.id.itemDistributorAddress, item?.Address)
    }
}


class AptitudeInfoAdapter(private val context: Context, layoutResId: Int, data: MutableList<FilePath>?) :
    BaseQuickAdapter<FilePath, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: FilePath?) {
        helper?.setImgLoad(context, R.id.itemAptitudeImg, item!!.FilePath, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
    }
}

class StockProductListAdapter(private val context: Context, layoutResId: Int, data: MutableList<StockProduct>?) :
    BaseQuickAdapter<StockProduct, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: StockProduct?) {
        helper?.setImgLoad(
            context,
            R.id.itemStockProductImg,
            item!!.ProductImage,
            R.mipmap.ic_launcher,
            R.mipmap.ic_launcher
        )
        helper?.setText(R.id.itemStockProductName, item?.ProductName)
            ?.setText(R.id.itemProductBrand, "产品品牌：" + item?.BrandName)
            ?.setText(R.id.itemProductPack, "产品规格：" + item?.NetUnitPack)
            ?.setText(R.id.itemStockNumberTV, "当前库存：")
            ?.setText(R.id.itemStockNumber, item?.NowQuality)
//                ?.setText(R.id.itemStockCompany, item?.PackUnitName)

        when (item?.StockType) {
            "1" -> helper?.setText(R.id.itemPutStorageState, "库存方式：单码入库")
            "2" -> helper?.setText(R.id.itemPutStorageState, "库存方式：二级码入库")
            "3" -> helper?.setText(R.id.itemPutStorageState, "库存方式：三级码入库")
        }
    }
}


class BatchOPeraSpinnerAdapter(context: Context, data: MutableList<BatchDetail>) :
    BaseSpinnerAdapter<BatchDetail>(context, data) {
    override fun convert(convertView: TextView, item: BatchDetail?) {
        convertView.text = item?.DetailName
    }
}

class AddImageAdapter(
    private val context: Context,
    layoutResId: Int,
    data: ArrayList<String>?,
    private var width: Int
) : BaseQuickAdapter<String, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: String?) {
        helper?.setRelativeLayoutWidth(R.id.itemAddImageLayout, width)
            ?.setRelativeLayoutHeight(R.id.itemAddImageLayout, width)
        helper?.addOnClickListener(R.id.itemDeleteTV)?.addOnClickListener(R.id.itemAddImage)
        if (item == "默认图片") {
            helper?.setImgLoad(
                context,
                R.id.itemAddImage,
                R.mipmap.default_add_img,
                R.mipmap.default_add_img,
                R.mipmap.default_add_img
            )
                ?.setGone(R.id.itemDeleteTV, false)
        } else {
            helper?.setImgLoad(context, R.id.itemAddImage, item!!, R.mipmap.ic_launcher, R.mipmap.ic_launcher)
                ?.setGone(R.id.itemDeleteTV, true)
        }
    }
}

class ReceiveNoticeAdapter(layoutResId: Int, data: MutableList<ReceiveNotice>?) :
    BaseQuickAdapter<ReceiveNotice, MyBaseViewHolder>(layoutResId, data) {
    override fun convert(helper: MyBaseViewHolder?, item: ReceiveNotice?) {
        helper?.setText(R.id.itemReceiveNoticeTitle, item?.NoticeTitle)
            ?.setText(R.id.itemReceiveNoticeTime, item?.CreateTime)
    }
}